{ "cells": [ { "cell_type": "markdown", "id": "956a0f98", "metadata": {}, "source": [ "# Functions & Variable Lifecycle — Code Cards (Classroom Version)\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Introduction/exercises/Functions%20and%20variable%20lifecycle%20code%20cards.ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FIntroduction%2Fexercises%2FFunctions%20and%20variable%20lifecycle%20code%20cards.ipynb)\n", "\n", "## How to use\n", "- Each card mirrors an A4 classroom prompt. **Predict first** (or discuss), then run the cell to check.\n", "- Detective cards show a buggy idea in Markdown; the code cell shows a **fixed** version.\n", "- Keep explanations short and schematic (*what* → *why*).\n", "\n", "### Turn Gemini into a coding tutor (no direct answers)\n", "Paste this in your first chat with Gemini to keep it in “tutor mode”:\n", "\n", "```\n", "You are a coding tutor for Python in Jupyter/Colab. Follow the course motto “do not give learning.”\n", "Role: Use Socratic guidance and test‑first thinking so I solve problems myself.\n", "\n", "Rules\n", "1) Do NOT provide full working solutions. Show at most tiny fragments (≤3 lines) or TODO-style pseudocode.\n", "2) Prefer questions over answers; offer one small next step at a time.\n", "3) When debugging, read the traceback, give 2–3 hypotheses, propose the smallest change in plain English.\n", "4) Encourage TDD: ask me to write an assert, predict, run, and report results.\n", "5) Keep replies concise (≈120–150 words) unless I ask for deep dives.\n", "6) Ask me to run code and share output; adapt based on results.\n", "7) If I request the full solution, remind me of the rules and offer a higher‑tier hint instead.\n", "```" ] }, { "cell_type": "markdown", "id": "126b4f71", "metadata": {}, "source": [ "## Key Concepts\n", "### Card KEY-1 — Code Wizard (Answer the question)\n", "Is `x = len(\"abc\")` an expression or a statement? Why? \n", "*(Discuss; then run to inspect the value.)*" ] }, { "cell_type": "code", "execution_count": null, "id": "53dd557a", "metadata": {}, "outputs": [], "source": [ "\n", "# KEY-1 — inspect\n", "x = len(\"abc\")" ] }, { "cell_type": "markdown", "id": "2ad893ea", "metadata": {}, "source": [ "### Card KEY-2 — Code Wizard (Answer the question)\n", "Is `y = abs(-8)` a function call, a variable assignment statement, or both? Explain in your own words. \n", "*(Discuss; then run to inspect the value.)*" ] }, { "cell_type": "code", "execution_count": null, "id": "a0462a6c", "metadata": {}, "outputs": [], "source": [ "\n", "# KEY-2 — inspect\n", "y = abs(-8)" ] }, { "cell_type": "markdown", "id": "d794aa7f", "metadata": {}, "source": [ "## Function Definition & Calls\n", "### Card FUN-1 — Code Wizard (Fix the code)\n", "Fix the code to **define a variable** `x`, assign `\"Hasta la vista baby\"`, and **print**\n", "```python\n", "x = print(\"Hasta la vista baby\")\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "f7a437e3", "metadata": {}, "outputs": [], "source": [ "\n", "# FUN-1 — Fix!\n", "x = print(\"Hasta la vista baby\")" ] }, { "cell_type": "markdown", "id": "a90b693f", "metadata": {}, "source": [ "### Card FUN-2 — Code Wizard (Fix the code)\n", "Define a polynomial function **f(x) = x^2 + 2x + 1** that **returns** the value. Then call `f(3)`.\n", "```python\n", "# FUN-2 — Fix!\n", "def f(x):\n", " y = x**2 + 1\n", "print(f(3)) ## Expected 16\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "781f4c57", "metadata": {}, "outputs": [], "source": [ "\n", "# FUN-2 — Fix!\n", "def f(x):\n", " y = x**2 + 1\n", "print(f(3)) ## Expected 16" ] }, { "cell_type": "markdown", "id": "62f2d4a2", "metadata": {}, "source": [ "### Card FUN-3 — Predict the output (default used vs overridden)\n", "```python\n", "def greet(name, punctuation=\"!\"):\n", " return f\"Hello, {name}{punctuation}\"\n", "\n", "print(greet(\"Ada\"))\n", "print(greet(\"Ada\", \"?\"))\n", "print(greet(name=\"Grace\"))\n", "```\n", "**Question:** What three lines are printed? Why?" ] }, { "cell_type": "code", "execution_count": null, "id": "92de3bac", "metadata": {}, "outputs": [], "source": [ "\n", "# FUN-3 — run to check\n", "def greet(name, punctuation=\"!\"):\n", " return f\"Hello, {name}{punctuation}\"\n", "\n", "print(greet(\"Ada\")) # default \"!\" used\n", "print(greet(\"Ada\", \"?\")) # override default\n", "print(greet(name=\"Grace\")) # keyword uses default" ] }, { "cell_type": "markdown", "id": "68d6f8b1", "metadata": {}, "source": [ "### Card FUN-4 — Predict the Output (positional & keyword)\n", "```python\n", "def power(base, exp=2):\n", " return base ** exp\n", "\n", "print(power(3))\n", "print(power(2, 3))\n", "print(power(exp=3, base=2))\n", "```\n", "**Question:** Predict the three numbers and explain which call used the default." ] }, { "cell_type": "code", "execution_count": null, "id": "8e124499", "metadata": {}, "outputs": [], "source": [ "# DEF-2 — run to check\n", "def power(base, exp=2):\n", " return base ** exp\n", "\n", "print(power(3))\n", "print(power(2, 3))\n", "print(power(exp=3, base=2))" ] }, { "cell_type": "markdown", "id": "9f0d71d1", "metadata": {}, "source": [ "### Card FUN-5 — Code Detective (mutable default trap)\n", "Buggy idea:\n", "```python\n", "def add_item(x, bag=[]):\n", " bag.append(x)\n", " return bag\n", "```\n", "**Task:** Explain the bug and **fix** so repeated calls don't share state.\n", "(**Hint:** WHat happens if bag is `None`)" ] }, { "cell_type": "code", "execution_count": null, "id": "a1d4bb74", "metadata": {}, "outputs": [], "source": [ "\n", "def add_item(x, bag=[]):\n", " bag.append(x)\n", " return bag\n", "\n", "print(add_item(\"a\"))\n", "print(add_item(\"b\"))\n", "print(add_item(\"c\", None))" ] }, { "cell_type": "markdown", "id": "092bd1dd", "metadata": {}, "source": [ "### Card FUN-6 — Mini‑Design: totals with defaults (tax & discount)\n", "Complete the code below to complete function `total(price, qty=1, tax=0.21, discount=0.0)` that returns the **final price** after applying discount first, then tax:\n", "`final = price * qty * (1 - discount) * (1 + tax)`\n", "\n", "Calls to predict:\n", "```python\n", "print(total(10))\n", "print(total(10, qty=3))\n", "print(total(10, tax=0.10, discount=0.20))\n", "print(total(price=10, discount=0.10, qty=2, tax=0.21))\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "b80b1b6f", "metadata": {}, "outputs": [], "source": [ "\n", "# FUN-6 — reference implementation & checks\n", "def total(price, qty=1, tax=0.21, discount=0.0):\n", " \"\"\"Return final cost after discount then tax.\"\"\"\n", "\n", "print(total(10)) # 12.1\n", "print(total(10, qty=3)) # 36.3\n", "print(total(10, tax=0.10, discount=0.20)) # 8.8\n", "print(total(price=10, discount=0.10, qty=2, tax=0.21)) # 21.78" ] }, { "cell_type": "markdown", "id": "2c263c84", "metadata": {}, "source": [ "### Card FUN7 — Code Wizard (Return vs Print)\n", "Consider:\n", "```python\n", "def area(w, h):\n", " print(w * h)\n", "a = area(3, 4)\n", "```\n", "**Question:** What is stored in `a`? Refactor to **return** the value and print at the call site." ] }, { "cell_type": "code", "execution_count": null, "id": "1cb0fa0c", "metadata": {}, "outputs": [], "source": [ "\n", "def area(w, h):\n", " #TODO: Fix to return the area\n", "\n", "a = area(3, 4)\n", "print(\"Area:\", a) # Expected: Area: 12" ] }, { "cell_type": "markdown", "id": "10276c61", "metadata": {}, "source": [ "### Card FUN-7 — Code Wizard Challenge (write & call with mixed arguments)\n", "Write `rectangle(w, h=1, unit=\"cm\") -> str` that **returns** a formatted string like `\"3x4 cm\"`. \n", "Then call it as:\n", "```python\n", "print(rectangle(3, 4)) # Expected 3x4 cm\n", "print(rectangle(5)) # Expected 5x1 cm\n", "print(rectangle(h=2, w=7, unit=\"mm\")) # Expected 2x7 cm\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "6b4e5843", "metadata": {}, "outputs": [], "source": [ "\n", "# DEF-7 — reference solution\n", "def rectangle(w, h=1, unit=\"cm\"):\n", " #TODO: Complete\n", "\n", "print(rectangle(3, 4)) # 3x4 cm\n", "print(rectangle(5)) # 5x1 cm\n", "print(rectangle(h=2, w=7, unit=\"mm\")) # 7x2 mm" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }